package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserColdRoomSite;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserColdRoomDaoImpl extends TempDataObjectDao<UserColdRoomSite> {

    public Map<Integer, UserColdRoomSite> getUserColdRoomSiteMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserColdRoomSite> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_coldroom_site where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserColdRoomSite data = parse(rs);
                dataMap.put(data.getSiteIndex(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
    
    @Override
    protected boolean addData(UserColdRoomSite data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_coldroom_site(userId,siteIndex,lastRestoreTime,beautyId) values(?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getSiteIndex());
            call.setLong(i++, data.getLastRestoreTime());
            call.setInt(i++, data.getBeautyId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserColdRoomSite data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_coldroom_site SET lastRestoreTime=?,beautyId=? where userId=? and siteIndex=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, data.getLastRestoreTime());
            call.setInt(i++, data.getBeautyId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getSiteIndex());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserColdRoomSite parseData(ResultSet rs) throws SQLException {
        UserColdRoomSite data = new UserColdRoomSite();
        data.setUserId(rs.getLong("userId"));
        data.setSiteIndex(rs.getInt("siteIndex"));
        data.setLastRestoreTime(rs.getLong("lastRestoreTime"));
        data.setBeautyId(rs.getInt("beautyId"));
        return data;
    }
}
